Visitor's IP and Country

  • STEP

    In Controller

    
                            public function  get_visitor_country(){
            
                                if(Cookie::has('VISITOR_IP_COUNTRY')){  
                                    $this->data['VISITOR_IP_COUNTRY']=Cookie::get("VISITOR_IP_COUNTRY"); 
                                }
                                else{
                                    $response=json_decode(file_get_contents("https://api.ipify.org/?format=json"),1);
                                    if(isset($response)){
                                        $response=json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$response['ip']),1);
                                        
                                        $this->data['VISITOR_IP_COUNTRY']=$response['geoplugin_countryCode'];
                                    }
                                }
                                
                            }
    
                        

    In view

    
                                            </script>